package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-15 10:44
 */
public class Solution_0415 {
    public static void main(String[] args) {
        String num1 = new String("123456");
        String num2 = new String("654321");
        Solution_0415 solution_0415 = new Solution_0415();
        solution_0415.addStrings(num1, num2);
        System.out.println(solution_0415.addStrings(num1, num2));
    }

    /**
     * m模板题
     *
     * @param num1
     * @param num2
     * @return
     */
    public String addStrings(String num1, String num2) {
        if (num1.length() == 0) {
            return num2;
        }
        if (num2.length() == 0) {
            return num1;
        }
        String res = "";
        //最好先判长度
        int i = num1.length() - 1, j = num2.length() - 1;
        int flag = 0;
        for (; i >= 0 & j >= 0; i--, j--) {
            int v0 = num1.charAt(i) - '0';
            int v1 = num2.charAt(j) - '0';
            int temp = flag + v0 + v1;
            flag = temp / 10;
            res = temp % 10 + res;
        }
        if (i < 0) {
            for (; j >= 0; j--) {
                int v1 = num2.charAt(j) - '0';
                int temp = v1 + flag;
                flag = temp / 10;

                res = temp % 10 + res;
            }
        }
        if (j < 0) {
            for (; i >= 0; i--) {
                int v1 = num1.charAt(i) - '0';
                int temp = v1 + flag;
                flag = temp / 10;

                res = temp % 10 + res;
            }
        }

        return flag == 1 ? 1+res  : res;
    }


}
